import React from 'react';
import {
  AsyncStorage,
} from 'react-native';

import Config from '../config';
import Constants from '../common/Constants';
import PostConstants from '../common/PostConstants';
import BaseModel from './BaseModel';

export default class ZahuopuModel extends BaseModel {
  constructor() {
    super();

    this.moduleType = Constants.MODULE_TYPE.ZAHUOPU;
    this.requestPosts = this.requestPosts.bind(this);
    this.createPost = this.createPost.bind(this);
    this.requestComment = this.requestComment.bind(this);
    this.requestContent = this.requestContent.bind(this);
    this.sendComment = this.sendComment.bind(this);
    this.sendSearch = this.sendSearch.bind(this);

    this.saveData = {requestPosts:{}};
  }

  //请求创新创效帖子
  //sort_id 排序方式，1最新，2最热
  //tag 1金点子，2曝光台，3勤俭模范
  //page 第几页
  //page_size 每页几项
  //http://118.89.111.124:8080/dqapp/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts?page=1&page_size=5&sort_id=1&tag=1
  requestPosts(actionData, callback) {
    var url = Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts?page=' + actionData.page +
      '&page_size=' + actionData.page_size + '&sort_id=' + actionData.sort_id + '&tag=' + actionData.tag;
    if(actionData.show_last_days) {
      url += "&show_last_days=" + actionData.show_last_days;
    }
    this.sendGet(url,
      (data, result)=>{this._requestPostsCallback(url, actionData, callback, data, result)});
  }

  _requestPostsCallback(url, actionData, callback, data, result) {
    // if(result == true) {
    //   this.saveData["requestPosts"][url] = data;
    //   AsyncStorage.setItem("requestPosts", JSON.stringify(this.saveData["requestPosts"]) ,function(errs){
    //               //TODO:错误处理
    //               if (errs) {
    //                 console.log('存储requestPosts错误');
    //               }
    //               if (!errs) {
    //                 console.log('存储requestPosts无误');
    //               }
    //           });
    // }
    callback(data, result, actionData.page);
  }

  //发表帖子 CreatePostPayload
  createPost(data, callback) {
    var sendData = {
      "content": data.content,
      "tag": parseInt(data.tag),
      "sub_tag": parseInt(data.sub_tag),
      "title": data.title,
      "anonymous": data.anonymous,
       "image_urls": data.image_urls,
    }
    this.sendPost(Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts', JSON.stringify(sendData), callback);
  }

  //board_id 版块ID, 目前只能填1，创新创效
  //page
  //page_size
  //post_id 帖子ID
  //http://118.89.111.124:8080/dqapp/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts/1111/comments?page=1&page_size=5
  requestComment(data, callback) {
    this.sendGet(Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts/' + data.post_id + "/comments?page=" + data.page + "&page_size=" + data.page_size,
      callback);
  }

  //请求内容
  requestContent(data, callback) {
    this.sendGet(Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts/' + data.post_id,
      callback);
  }

  //发表评论
  //http://118.89.111.124:8080/dqapp/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts/11111/comments
  sendComment(data, callback) {
    var sendData = {
      "content": data.comment,
    }
    this.sendPost(Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts/' + data.post_id + '/comments', JSON.stringify(sendData), callback);
  }

  //搜索http://118.89.111.124:8080/dqapp/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts/searched?key=1&page=1&page_size=5&sub_tag=1&tag=1
  // sub_tag (integer, optional): 子标签，从1开始，分别为原油开发、天然气开发、钻探工程、工程建设、机械制造、生产保障、石油化工、企业管理、安全环保、质量节能、科技创新、市场开发、人才培养、企业文化、党建工作、信息技术、矿区服务
  sendSearch(data, callback) {
    if(data.searchText == ""){
       this.sendGet(Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts/searched?page=' + data.page + "&page_size=10&sub_tag=" + data.sub_tag + "&tag=" + data.tag,
      callback);
       return;
    }
    this.sendGet(Config.serverUrl + '/boards/' + PostConstants.BOARD_ID.ZAHUOPU + '/posts/searched?key=' + data.searchText + "&page=" + data.page + "&page_size=10&sub_tag=" + data.sub_tag + "&tag=" + data.tag,
      callback);
  }
}
